package com.bear.cmcc.NetTestHTTP.ui.adapter;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.CityBean;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class CityTopListAdapter extends BaseAdapter {

	private Activity mContext;

	private ArrayList<CityBean> mBeans;
	
	private Boolean isTD;

	public CityTopListAdapter(Activity context,
			ArrayList<CityBean> beans, Boolean isTD) {
		this.mContext = context;
		this.mBeans = beans;
		this.isTD = isTD;
	}

	@Override
	public int getCount() {
		return mBeans.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = makeView();
		}
		convertView.setMinimumHeight(60);
		Holder holder = (Holder) convertView.getTag();
		CityBean bean = mBeans.get(position);

		holder.tvNo.setText(String.valueOf(position+1));
		holder.tvCity.setText(bean.getCity_name());
		if(isTD)
		{
			holder.tvSpeed.setText(bean.getDownSpeed()+"");			
		}
		else
		{
			holder.tvSpeed.setText(bean.getDownSpeed()+"");		
		}
		holder.lyItem = (LinearLayout) convertView.findViewById(R.id.fixed);
		holder.lyIteminfo = (LinearLayout) convertView.findViewById(R.id.slide);
		holder.tvCallComplete.setText(bean.getComRate());
//		holder.tvElectric.setText(String.valueOf(bean.getElectricalLevel()));
		holder.tvDrapRate.setText(String.valueOf(bean.getDroppingRate()));
		holder.tvSignal.setText(String.valueOf(bean.getSignalStrength()));
		
		if(position%2==0){
			convertView.setBackgroundResource(R.drawable.bg_flutter_list);
		}else{
			convertView.setBackgroundResource(R.drawable.black_background);
		}
		
		return convertView;
	}

	private View makeView() {
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.item_hvlist, null);
		Holder holder = new Holder();
		holder.tvNo = (TextView) view.findViewById(R.id.start_item_no);
		holder.tvCity = (TextView) view.findViewById(R.id.start_item_city);
		holder.tvSpeed = (TextView) view.findViewById(R.id.start_item_speed);
		
//		holder.tvElectric = (TextView) view.findViewById(R.id.slide_item_electric);
		holder.tvDrapRate = (TextView) view.findViewById(R.id.slide_item_drop_rate);
		holder.tvCallComplete = (TextView) view.findViewById(R.id.slide_item_call_complete);
		holder.tvSignal = (TextView) view.findViewById(R.id.slide_item_signal);

		view.setTag(holder);
		return view;
	}

	private class Holder {
		/**
		 * 序号
		 */
		TextView tvNo;

		/**
		 * 城市
		 */
		TextView tvCity;

		/**
		 * 速率
		 */
		TextView tvSpeed;

		/**
		 * 电频值
		 */
//		TextView tvElectric;
		
		/**
		 * 掉线率
		 */
		TextView tvDrapRate;
		/**
		 * 信号强度
		 */
		TextView tvSignal;
		/**
		 * 接通率
		 */
		TextView tvCallComplete;
		LinearLayout lyItem;
    	LinearLayout lyIteminfo;
	}
}
